<?php

/**
 * @author ducnhat
 * @email ddnhat@gmail.com
 * @copyright 2014
 */
 
class Admin_ProductCategoryController extends Zenddn_Controller_Action{
	
	public function init(){
		parent::init();
        $this->loadTemplate('alpona');
        $this->view->headTitle = "category name";
	}
	
	public function indexAction(){
	    $scripts = array('jquery.dataTables.js', 'dataTables.bootstrap.js', 'TableTools.js');
		parent::addScripts($scripts);
        
	    $model = new Admin_Model_Taxonomy();
        $data = $model->getAllData('product');
        
        $this->view->data = $data;
        $this->view->controller = 'product-category';
	}
    
    public function addAction(){
    	$request = $this->_request;
        
        if($request->isPost()){
            $this->save($request->getParams());
        }
        
        $this->view->type = 'product';
    }
    
    public function editAction(){
    	$request = $this->_request;
        
        $id = $request->getParam('id');
        
        $model = new Admin_Model_Taxonomy();
        $data = $model->getData('product', $id);
        
        if($data){
            $this->view->data = $data;
        }
        
        if($request->isPost()){
            $this->save($request->getParams(), $data);
        }
        
        $this->view->type = 'product';
    }
    
    public function deleteAction(){
    	$request = $this->_request;
        
        $id = $request->getParam('id');
        
        $model = new Admin_Model_Taxonomy();
        $model->deleteData($id);
        
        $this->_helper->_redirector->gotoRoute(array('module' => 'admin', 'controller' => 'product-category', 'action' => 'index'), null, true);
        
        $this->_helper->viewRenderer->setNoRender();
    }
    
    private function save($params, $data = null){
    	$validate = new Admin_Form_Taxonomy($params, $data);
            
        if($validate->isError()){
            $this->view->classError = $validate->getClassError();
            $this->view->messageError = $validate->getMessageError();
            $this->view->data = $validate->getData();
        }else{
            $model = new Admin_Model_Taxonomy();
            $model->saveData($validate->getData());
            
            $this->_helper->_redirector->gotoRoute(array('module' => 'admin', 'controller' => 'product-category', 'action' => 'index'), null, true);
        }
    }
}